package com.tobby.camel.camelinaction.chapter6.firsttest;

import java.io.File;

import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.properties.PropertiesComponent;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class CamelRiderJavaDSLProdTest extends CamelTestSupport {

	private String inboxDir;

	private String outboxDir;

	@EndpointInject(uri = "file:{{file.inbox}}")
	private ProducerTemplate inbox;

	public void setUp() throws Exception {
		super.setUp();

		inboxDir = context.resolvePropertyPlaceholders("{{file.inbox}}");
		outboxDir = context.resolvePropertyPlaceholders("{{file.outbox}}");

		deleteDirectory(inboxDir);
		deleteDirectory(outboxDir);
	}

	@Override
	protected CamelContext createCamelContext() throws Exception {
		CamelContext context = super.createCamelContext();
		PropertiesComponent prop = context.getComponent("properties",
				PropertiesComponent.class);
		prop.setLocation(
				"classpath:com/tobby/camel/camelinaction/chapter6/firsttest/rider-prod.properties");
		return context;
	}

	@Override
	protected RouteBuilder createRouteBuilder() {
		return new RouteBuilder() {

			@Override
			public void configure() throws Exception {
				from("file:{{file.inbox}}").to("file:{{file.outbox}}");
			}
		};
	}

	@Test
	public void testMoveFile() throws InterruptedException {
		inbox.sendBodyAndHeader("Hello World",
				Exchange.FILE_NAME, "hello.txt");

		Thread.sleep(2000);

		File target = new File(outboxDir + "/hello.txt");
		assertTrue("File not moved", target.exists());

		String content = context.getTypeConverter().convertTo(String.class, target);
		assertEquals("Hello World", content);
	}

}
